package com.zf.yichat.service.impl;

import com.zf.yichat.service.SecurityService;
import com.zf.yichat.service.config.ConfigSet;
import com.zf.yichat.service.config.RedisService;
import com.zf.yichat.utils.common.AesUtil;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
 * 一句话描述功能
 *
 * @author fengsong
 * @date create in 16:07 2019/6/3 2019
 */
@Service
public class SecurityServiceImpl implements SecurityService {

    @Autowired
    private ConfigSet configSet;

    @Autowired
    private RedisService redisService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String token(String salt, Long userId) throws Exception {
        String token = AesUtil.encrypt(salt + ":" + userId, configSet.getSecurity().getAesKey().getToken());
        redisService.setToken(userId, token);
        return token;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String token2(String salt, Long userId) throws Exception {
        long time = System.currentTimeMillis();
        String token = AesUtil.encrypt(salt + ":" + userId + ":" + time, configSet.getSecurity().getAesKey().getToken());
        redisService.setToken2(userId, time, token);
        return token;
    }

    @Override
    public String validToken(String token) {
        if (StringUtils.isBlank(token)) {
            return "登录口令为空";
        }
        try {
            String de = AesUtil.decrypt(token, configSet.getSecurity().getAesKey().getToken());
            if (StringUtils.isNotBlank(de)) {
                Long userId = NumberUtils.toLong(de.split(":")[1]);
                String oldToken = redisService.getToken(userId);
                if (StringUtils.isNotBlank(oldToken)) {
                    if (oldToken.equals(token)) {
                        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                        HttpSession session = requestAttributes.getRequest().getSession();
                        session.setAttribute("userId", userId);
                        return "success";
                    } else {
                        return "已在其他设备登录";
                    }
                } else {
                    return "登录已过期";
                }
            } else {
                System.out.println(token);
                return "登录错误";
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(token);
            return "登录异常";
        }
    }

    @Override
    public FsResponse getUserIdByToken(String token) {
        if (StringUtils.isBlank(token)) {
            return FsResponseGen.fail("003", "登录口令为空");
        }
        try {
            String de = AesUtil.decrypt(token, configSet.getSecurity().getAesKey().getToken());
            if (StringUtils.isNotBlank(de)) {
                Long userId = NumberUtils.toLong(de.split(":")[1]);
                String oldToken = redisService.getToken(userId);
                if (StringUtils.isNotBlank(oldToken)) {
                    if (oldToken.equals(token)) {
                        return FsResponseGen.successData(userId);
                    } else {
                        return FsResponseGen.fail("003", "已在其他设备登录");
                    }
                } else {
                    return FsResponseGen.fail("003", "登录已过期");
                }
            } else {
                System.out.println(token);
                return FsResponseGen.fail("003", "登录错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(token);
            return FsResponseGen.fail("003", "登录异常");
        }
    }

    @Override
    public String validToken2(String token) {
        if (StringUtils.isBlank(token)) {
            return "登录口令为空";
        }
        try {
            String de = AesUtil.decrypt(token, configSet.getSecurity().getAesKey().getToken());
            if (StringUtils.isNotBlank(de)) {
                Long userId = NumberUtils.toLong(de.split(":")[1]);
                Long time = NumberUtils.toLong(de.split(":")[2]);
                String oldToken = redisService.getToken2(userId, time);
                if (StringUtils.isNotBlank(oldToken)) {
                    if (oldToken.equals(token)) {
                        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                        HttpSession session = requestAttributes.getRequest().getSession();
                        session.setAttribute("userId", userId);
                        return "success";
                    } else {
                        return "已在其他设备登录";
                    }
                } else {
                    return "登录已过期";
                }
            } else {
                System.out.println(token);
                return "登录错误";
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(token);
            return "登录异常";
        }
    }

    @Override
    public void clearToken(Long userId) {
        redisService.deleteToken(userId);
    }

    @Override
    public void clearToken2(String token) {
        try {
            String de = AesUtil.decrypt(token, configSet.getSecurity().getAesKey().getToken());
            if (StringUtils.isNotBlank(de)) {
                Long userId = NumberUtils.toLong(de.split(":")[1]);
                Long time = NumberUtils.toLong(de.split(":")[2]);
                redisService.deleteToken2(userId, time);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean checkToken(String token) {
//        if (StringUtils.isBlank(token)) {
//            return false;
//        }
//        try {
//            String de = AesUtil.decrypt(token, configSet.getSecurity().getAesKey().getToken());
//            if (StringUtils.isNotBlank(de)) {
//                Long userId = NumberUtils.toLong(de.split(":")[1]);
//                return redisService.valid(userId, token);
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return false;
    }
}
